import { create } from 'zustand';
import * as R from 'ramda';


const useFlowStore = create()(
  (set, get) => ({
    data: {
      flowKey: '',
      flowName: '',
      flowType: '',
      flowContent: [],
      flowInputParams: [],
      flowOutputParams: [],
      flowVariables: [],
      id: null,
      remark: '',
    },
    getFlowNodes() {
      return get().data.flowContent;
    },

    getFlowNode(key) {
      return get().data.flowContent.find(node => node.key === key);
    },

    updateFlowNode(key, newData) {
      set(R.over(
        R.lensProp('flowContent'),
        R.ifElse(
          R.any(R.propEq('key', key)),
          R.map(R.when(R.propEq('key', key), R.always(newData))),
          R.identity
        ),
        get().data
      ))
    },
    update(updater) {
      const data = R.clone(get().data)
      updater(data)
      set({ data })
    }
  })
);

export const useFlowData = () => useMenuStore((state) => state.data);


export default useFlowStore;
